package com.yc.fav.servlet;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.base.servlet.BaseServlet;
import com.yc.fav.entity.Fav;
import com.yc.fav.entity.User;
import com.yc.fav.mapper.FavMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.example.util.MyBatisUtils;

import java.io.IOException;
import java.util.List;

@WebServlet("/fav/*")
public class FavServlet extends BaseServlet{

    public Object myFavAll(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("loginedUser");
        if(user == null){
            return null;
        }
        try(SqlSession sqlSession = MyBatisUtils.getSession()){
            FavMapper favMapper = sqlSession.getMapper(FavMapper.class);
            LambdaQueryWrapper<Fav> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Fav::getUid, user.getId());
            List<Fav> list = favMapper.selectList(lqw);
            return list;
        }
    }

}
